package com.weiweiqin.controller.home;

import java.util.List;

import com.weiweiqin.utils.EmojiUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.weiweiqin.model.Category;
import com.weiweiqin.service.CategoryService;
import com.weiweiqin.service.GoodsService;
import com.weiweiqin.vo.common.Result;

@Controller
@RequestMapping("front/category")
public class FCategoryController {

	private static final Logger logger = LoggerFactory.getLogger(FCategoryController.class);
	
	@Autowired
	private CategoryService categoryService;
	@Autowired
	private GoodsService goodsService;

	@RequestMapping(value = "/getAll.do", method = { RequestMethod.POST,
			RequestMethod.GET })
	@ResponseBody
	public String getAll() {
		Result result = new Result();
		try {
			List<Category> categoryList = categoryService.getAll();
			result.setObj(categoryList);
		} catch (Exception e) {
			logger.error("数据库异常...", e);
			result.setResult(false);
			result.setErrorMsg("数据库异常。。。");
			return result.toJson();
		}
		result.setResult(true);
		return result.toJson();
	}

	@RequestMapping(value = "/get.do", method = { RequestMethod.POST,
			RequestMethod.GET })
	@ResponseBody
	public String edit(int id) {
		logger.info("Enter method edit...,id="+id);
		Result result = new Result();
		try {
			Category category = categoryService.get(id);
			category.setContent(EmojiUtil.emojiRecovery2(category.getContent()));
			result.setObj(category);
		} catch (Exception e) {
			logger.error("数据库异常...", e);
			result.setResult(false);
			result.setErrorMsg("数据库异常。。。");
			return result.toJson();
		}
		result.setResult(true);
		return result.toJson();
	}
	
	@RequestMapping(value = "/getAlllAddOn.do", method = { RequestMethod.POST,
			RequestMethod.GET })
	@ResponseBody
	public String getAllAddOn() {
		Result result = new Result();
		try {
			List<Category> categoryList = categoryService.getAllAddOn();
			result.setObj(categoryList);
		} catch (Exception e) {
			logger.error("数据库异常...", e);
			result.setResult(false);
			result.setErrorMsg("数据库异常。。。");
			return result.toJson();
		}
		result.setResult(true);
		return result.toJson();
	}
	
	
}
